import Vue from 'vue'
import Vuex from 'vuex'
import $helpers from '@/common/utils/helpers.js';
import $storageKeyEnum from '@/common/enum/storage-key.enum.js'

const TOKEN = $helpers.getCache($storageKeyEnum.token_cache);
const USER_INFO = $helpers.getCache($storageKeyEnum.userInfo);

Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		userInfo: USER_INFO,
		recentUrl: null,
		router: null,
		token: TOKEN,
		params: null,
		promptsForLogin: false
	},
	mutations: {
		SET_TOKEN(state, token) {
			state.token = token;
			$helpers.setCache($storageKeyEnum.token_cache, token)
		},
		// 用户信息
		SET_USER_INFO(state, userInfo) {
			state.userInfo = userInfo;
			$helpers.setCache($storageKeyEnum.userInfo, userInfo)
		},
		SET_PROMPTS_FOR_LOGIN(state, data) {
			state.promptsForLogin = data;
			$helpers.setCache($storageKeyEnum.prompts, data, 2)
		},
	},
	getters: {
		GET_VALUE: (state) => (property) => {
			if (property == 'GET_USER_INFO') {
				return state.userInfo
			} else if (property == 'GET_TOKEN') {
				return state.token
			} else if (property == 'GET_PROMPTS_FOR_LOGIN') {
				return state.promptsForLogin
			} else {
				return null
			}
		}
	}
})

export default store